package com.example.nft.controller;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Demo2Service {

    public static void main(String[] args) {
//        JSONObject jsonObject = new JSONObject ();
//        List<String> texthtmls = new ArrayList<> ();
//        String url = "https://b.sis.la/";
//        List<String> textHtml = getTextHtmls (url);


        LocalDateTime localDateTime = LocalDateTime.now ().plusDays (3);

//        long time = dateTime.getTime ();
        System.out.println (localDateTime.toString ().substring (0,10));
//        String url1 = "https://www.338e.net/pic/renqi/";
//        String url2 = "https://www.338e.net/pic/dushi/";
//        String url3 = "https://www.338e.net/pic/luanlun/";
//        texthtmls.add (url);
//        texthtmls.add (url1);
//        texthtmls.add (url2);
//        texthtmls.add (url3);
//        for (String videohtml : texthtmls) {
//            if (videohtml.contains (":void(0)")){
//                continue;
//            }
//            List<String> textHtml = getTextHtml (videohtml);
//            for (String s : textHtml) {
//                if (s.contains (":void(0)")){
//                    continue;
//                }
//                getText (s,jsonObject);
//            }
////            logger.info ("小说全部存入完成");
//        }
//        System.out.println (jsonObject);
    }


    //解析页面
    public static String getHtml(String url){
        String html = "";
        try {
            html = Jsoup.connect(url).execute().body();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return html;
    }
    //获取详细页面的最大值
    public static Integer getindex(String html){
        String html1 = getHtml(html);
        String s ="https://www.338e.net";
        Document document = Jsoup.parse(html1);
        Elements a1 = document.select(".moble_pagination").select("a");
        String index =s+a1.get(a1.size() - 1).attr("href");
        String index_ = index.replace(html, "").replace("index_", "").replace(".html", "");
        return Integer.valueOf(index_);
    }

    //获取首页子页面
    public static List<String> getTextHtmls(String html){
        html = getHtml(html);
        List<String> list = new ArrayList<> ();
        String s =null;
        Document document = Jsoup.parse(html);
        Elements wrap = document.select (".widget-panel");
        for (Element element : wrap) {
            Elements a = element.select ("a");
            for (Element element1 : a) {
                String attr = element1.select ("a").attr ("href");
                list.add ("https://www.338e.net"+attr);
            }
        }
        return list;
    }

    //获取小说页面
    public static List<String> getTextHtml(String html){
        Integer getindex = getindex(html);
        List<String> list = new ArrayList<> ();
        for (int i = 1; i<=10; i++) {
//        for (int i = 1; i<=2; i++) {
            StringBuilder htmlBuilder = new StringBuilder (html);
            htmlBuilder.append ("index_").append (i).append (".html");
            String s = htmlBuilder.toString ();
            if (s.contains ("index_1")) {
                s = s.replace ("index_1.html", "");
            }
            s = getHtml (s);
            Document document = Jsoup.parse(s);
            Element select = document.select (".wrap").get (1);
            Elements dl = select.getElementsByTag ("dl");
            for (Element element : dl) {
                String attr = element.select ("a").attr ("href");
                list.add ("https://www.338e.net"+attr);
            }
        }
        return list;
    }
    public static JSONObject getText(String html,JSONObject jsonObject){
        String html1 = getHtml(html);
        Document document = Jsoup.parse(html1);
        String title = document.getElementsByTag("title").get(0).text();
        Element select = document.select (".wrap").get (1);
        String p = select.getElementsByTag ("p").get (1).text ();
        String h2 = select.getElementsByTag ("h2").text ().trim ();

        System.out.println (p);
        jsonObject.put ("title",title);
        jsonObject.put ("content", p);
        jsonObject.put ("fenqu",h2.substring (0,4));
        jsonObject.put ("date",h2.substring (4).trim ());

        return jsonObject;
    }


    //获取首页子页面
    public static List<String> getHtmls(String html){
        html = getHtml(html);
        List<String> list = new ArrayList<> ();
        String s =null;
        Document document = Jsoup.parse(html);
        Elements wrap = document.select (".wrap");
        for (Element element : wrap) {
            Elements a = element.select ("a");
            for (Element element1 : a) {
                String attr = element1.select ("a").attr ("href");
                list.add ("https://www.338e.net"+attr);
            }
        }
        return list;
    }
    //获取视频详细页面
    public static List<String> getVideoHtml(String html){
        Integer getindex = getindex(html);
        List<String> list = new ArrayList<> ();
        for (int i = 1; i<=10; i++){
            StringBuilder htmlBuilder = new StringBuilder(html);
            htmlBuilder.append("index_").append(i).append(".html");
            String s = htmlBuilder.toString();
            if (s.contains ("index_1")) {
                s = s.replace ("index_1.html", "");
            }
            s = getHtml(s);
            Document document = Jsoup.parse(s);
            Elements wrap = document.select (".preview-item");
            for (Element element : wrap) {
                Elements a = element.select ("a");
                for (Element element1 : a) {
                    String attr = element1.select ("a").attr ("href");
                    list.add ("https://www.338e.net"+attr);
                }
            }
        }
        return list;

    }
    //获取具体视频
    public static JSONObject getVideo(String html, JSONObject jsonObject){
        String html1 = getHtml(html);
        Document document = Jsoup.parse(html1);
        Elements wrap = document.select (".download");
        String title = document.getElementsByTag("title").get(0).text();
        Elements select = document.select (".pannel.clearfix");
        String data  =  select.select (".data").get (1).text ().replace ("时间：","");
        String fenqu = select.select (".data").get (2).text().replace ("分类：","");
        for (Element element : wrap) {
            Elements a = element.select ("a");
            for (Element element1 : a) {
                String attr = element1.select ("a").attr ("href");
                jsonObject.put ("title",title);
                jsonObject.put ("address",attr);
                jsonObject.put ("datetime",data);
                jsonObject.put ("fenqu",fenqu);
            }
        }
        return jsonObject;
    }
}
